package com.lumlord.common.spring.rpc;
import com.lumlord.common.spring.SpringContextUtil;
import com.lumlord.util.RestTemplateUtil;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.context.ApplicationContext;
import org.springframework.core.env.Environment;


/**
 * 网桥 代理工廠
 * @author GPF
 */
public  class LumlordNetBridge<T> implements FactoryBean<T> {
    private Class<T> interfaceClass;
    private Environment environment;



    private ApplicationContext applicationContext;

    public Class<T> getInterfaceClass() {
        return interfaceClass;
    }
    public void setInterfaceClass(Class<T> interfaceClass) {
        this.interfaceClass = interfaceClass;
    }
    @Override
    public T getObject() throws Exception {
        LoadBalancerClient loadBalancerClient = null == applicationContext?null: applicationContext.getBean(LoadBalancerClient.class);
        RestTemplateUtil restTemplateUtil = null == applicationContext?null: applicationContext.getBean(RestTemplateUtil.class);
        return (T) new LumlordNetBridgeProxy().bind(interfaceClass,environment,loadBalancerClient,restTemplateUtil);
    }

    @Override
    public Class<?> getObjectType() {
        return interfaceClass;
    }

    @Override
    public boolean isSingleton() {
        // 单例模式
        return true;
    }

    public void setApplicationContext(ApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
    }

    public void setEnvironment(Environment environment) {
        this.environment = environment;
    }
}